using System;

namespace Legalsoft.Truffer
{
    public class Matrix3D<T> : System.IDisposable
    {
        private int nn { get; set; } = 0;
        private int mm { get; set; } = 0;
        private int kk { get; set; } = 0;
        private T[] v { get; set; } = null;

        public Matrix3D()
        {
            this.nn = 0;
            this.mm = 0;
            this.kk = 0;
            this.v = null;
        }

        public Matrix3D(int n, int m, int k)
        {
            this.nn = n;
            this.mm = m;
            this.kk = k;
            this.v = (n * m * k > 0) ? new T[n * m * k] : null;
        }

        public T this[int i]
        {
            get
            {
                return v[i];
            }
            set
            {
                v[i] = value;
            }
        }

        public T this[int i, int j, int k]
        {
            get
            {
                return v[i * mm * kk + j * kk + k];
            }
            set
            {
                v[i * mm * kk + j * kk + k] = value;
            }
        }

        public T[] ToArray()
        {
            int dim = dim1() * dim2() * dim3();
            T[] r = new T[dim];
            for (int i = 0; i < dim; i++)
            {
                r[i] = this[i];
            }
            return r;
        }

        public void FromArray(T[] r)
        {
            for (int i = 0; i < r.Length && i < v.Length; i++)
            {
                this[i] = r[i];
            }
        }

        public int dim1()
        {
            return nn;
        }


        public int dim2()
        {
            return mm;
        }


        public int dim3()
        {
            return kk;
        }

        public void Dispose()
        {
            if (v != null)
            {
                v = null;
            }
        }
    }
}
